package com.example.admin.yuejian_22.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.yuejian_22.Bean.ClubList;
import com.example.admin.yuejian_22.Bean.FanUser;
import com.example.admin.yuejian_22.Fragment.ClubDetail_AFragment;
import com.example.admin.yuejian_22.Fragment.ClubDetail_BFragment;
import com.example.admin.yuejian_22.Fragment.ClubDetail_CFragment;
import com.example.admin.yuejian_22.Listener.UserListener;
import com.example.admin.yuejian_22.Model.UserModel;
import com.example.admin.yuejian_22.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ClubDetailActivity extends AppCompatActivity  implements View.OnClickListener {


    private RadioButton radio_a;
    private RadioButton radio_b;
    private RadioButton radio_c;
    private ImageButton img_back;
    private ImageButton ic_collect;
    private ImageView img_club_detail;
    private TextView Club_name;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private ClubDetail_AFragment clubDetail_aFragment;
    private ClubDetail_BFragment clubDetail_bFragment;
    private ClubDetail_CFragment clubDetail_cFragment;
    private Context Context;
    private String userid;
    private String fan_id;
    private int k=0;
    private  ClubDetailActivity act = this;

    private UserListener userListener = new UserListener() {
        @Override
        public void onResponse(Object beanobject) {

            if((Integer)beanobject==0){
                k=1;
                ic_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect1));
            }
            else{
                k=0;
                ic_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect));
            }


        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(act, ""+msg, Toast.LENGTH_SHORT).show();
        }
    };


    private UserListener userListener1 = new UserListener() {
        @Override
        public void onResponse(Object beanobject) {
            Toast.makeText(act, "收藏成功", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(act, "失败", Toast.LENGTH_SHORT).show();
        }
    };

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
        userid = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取id=========", String.valueOf(userid));
        radio_a = (RadioButton) findViewById(R.id.radio_a);
        radio_b = (RadioButton) findViewById(R.id.radio_b);
        radio_c = (RadioButton) findViewById(R.id.radio_c);
        img_back = (ImageButton)findViewById(R.id.img_back);
        ic_collect = (ImageButton)findViewById(R.id.ic_collect);
        Club_name = (TextView)findViewById(R.id.club_name_detail);
        img_club_detail = (ImageView) findViewById(R.id.img_club_detail);


        Intent intent = getIntent();
        Club_name.setText(intent.getStringExtra("clubname"));
        userid = intent.getStringExtra("userid");
        fan_id = intent.getStringExtra("Clubid");
        String club_photo = intent.getStringExtra("thumb");
        String address="http://172.24.10.175/workout/Uploads/"+club_photo;
        Picasso.with(Context).load(address).into(img_club_detail);


        UserModel model = new UserModel();
        model.ifgz(userid,fan_id,userListener);



        fragmentManager =getSupportFragmentManager();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ic_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(k==0){
                    k=1;
                    UserModel model = new UserModel();
                    model.clubfan(userid,fan_id,userListener1);
                    ic_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect1));
                }
                else {
                    k=0;
                    UserModel model = new UserModel();
                    model.clubfan(userid,fan_id,userListener1);
                    ic_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect));
                }

            }
        });

        radio_a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    clubDetail_aFragment = new ClubDetail_AFragment();
                    replageFragment(clubDetail_aFragment);
                }
            }
        });

        radio_b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    clubDetail_bFragment = new ClubDetail_BFragment();
                    replageFragment(clubDetail_bFragment);
                }
            }
        });

        radio_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    clubDetail_cFragment = new ClubDetail_CFragment();
                    replageFragment(clubDetail_cFragment);
                }
            }
        });
    }
    private void replageFragment(Fragment fragment){
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder_club,fragment);
        transaction.commit();
    }


    @Override
    public void onClick(View view) {

    }
}
